package flex.cc.uac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.annotation.ValidateAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.uac.dto.ModifyStatusDto;
import flex.cc.uac.entity.UacActionEntity;
import flex.cc.uac.service.UacActionService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 权限表
 *
 * @author zhangzk
 * @email zhangzhikai@sinosoft.com.cn
 * @date 2020-07-11 22:29:37
 */
@RestController
@RequestMapping(value = "/uac/action")
public class UacActionController extends BaseController {

    @Resource
    private UacActionService uacActionService;

    /**
     * 分页查询权限信息.
     */
    @PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询角色列表")
    public Wrapper<IPage<UacActionEntity>> queryUacActionListWithPage(@ApiParam(name = "action", value = "权限信息")
                                                                          @RequestBody UacActionEntity action) {
        logger.info("查询角色列表actionQuery={}", action);
        IPage<UacActionEntity> page = uacActionService.queryActionListWithPage(action);
        return WrapMapper.ok(page);
    }

    /**
     * 删除权限信息.
     */
    @DeleteMapping(value = "/deleteActionById/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "删除权限")
    @LogAnnotation
    public Wrapper deleteUacActionById(@ApiParam(name = "id", value = "资源id") @PathVariable Long id) {
        int result = uacActionService.deleteActionById(id);
        return super.handleResult(result);
    }

    /**
     * 获得权限信息.
     */
    @GetMapping(value = "/getActionById/{id}")
    @ApiOperation(httpMethod = "GET", value = "删除角色")
    public Wrapper<UacActionEntity> getUacActionById(@ApiParam(name = "id", value = "资源id") @PathVariable Long id) {
        UacActionEntity uacAction = uacActionService.getById(id);
        return WrapMapper.ok(uacAction);
    }

    /**
     * 保存权限信息.
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增权限")
    @ValidateAnnotation
    @LogAnnotation
    public Wrapper save(@ApiParam(name = "action", value = "角色信息") @RequestBody UacActionEntity uacAction) {
        LoginAuthDto loginAuthDto = this.getLoginAuthDto();
        uacActionService.saveAction(uacAction, loginAuthDto);
        return WrapMapper.ok();
    }

    /**
     * 根据权限Id修改角色状态.
     */
    @PostMapping(value = "/modifyActionStatus")
    @ApiOperation(httpMethod = "POST", value = "根据权限Id修改权限状态")
    @LogAnnotation
    public Wrapper modifyActionStatus(@ApiParam(name = "modifyActionStatus", value = "修改权限状态") @RequestBody ModifyStatusDto modifyStatusDto) {
        logger.info("根据权限Id修改权限状态 modifyStatusDto={}", modifyStatusDto);
        Long actionId = modifyStatusDto.getId();
        Preconditions.checkArgument(actionId != null, "权限ID不能为空");

        UacActionEntity uacAction = new UacActionEntity();
        uacAction.setId(actionId);
        uacAction.setStatus(modifyStatusDto.getStatus());
        uacAction.setUpdateInfo(getLoginAuthDto());
        boolean result = uacActionService.updateById(uacAction);
        return super.handleResult(result);
    }
}
